package com.feicheng.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.base.common.DaoConstant;
import com.feicheng.base.exception.SystemException;
import com.feicheng.base.result.Result;
import com.feicheng.entity.Product;
import com.feicheng.entity.Stock;
import com.feicheng.model.ProductAndNum;
import com.feicheng.model.ProductAndUser;
import com.feicheng.model.WarehouseWithStore;
import com.feicheng.service.IProductService;
import com.feicheng.service.IStockService;
import com.feicheng.service.IWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hao.zhang
 * @since 2022-07-04
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    // 依赖注入商品表service
    @Autowired
    private IProductService iProductService;


    // 依赖注入库存service
    @Autowired
    private IStockService iStockService;

    // 依赖注入仓库表service
    @Autowired
    private IWarehouseService iWarehouseService;

    /**
     * 出库商品名称二级联动下拉列表查询
     * @param wareHouse 仓库主键
     * @param
     * @return
     */
    @PostMapping("/a")
    public Result selectProductName(
            @RequestBody WarehouseWithStore wareHouse
    ) {

        if (null == wareHouse.getWarehouseId()) {

            throw new SystemException("缺少仓库主键");
        }

        if (null == wareHouse.getType()) {

            throw new SystemException("缺少商品类型");
        }

        QueryWrapper<Stock> stockQueryWrapper = new QueryWrapper<>();

        stockQueryWrapper.eq(DaoConstant.STOCK.WAREHOUSE_ID, wareHouse.getWarehouseId());

        List<Stock> stockList = iStockService.list(stockQueryWrapper);

        List<Stock> stockListNext = iStockService.list(stockQueryWrapper);

        List<Integer> productIds = new ArrayList<>();

        for (Iterator<Stock> iterator = stockList.iterator(); iterator.hasNext();) {

            productIds.add(iterator.next().getProductId());
        }

        QueryWrapper<Product> productQueryWrapper =
                new QueryWrapper<>();

        productQueryWrapper.in(DaoConstant.PRODUCT.PRODUCT_ID, productIds);

        productQueryWrapper.eq(DaoConstant.PRODUCT.TYPE, wareHouse.getType());

        List<Product> productslist = iProductService.list(productQueryWrapper);

        List<ProductAndNum> productAndNums = new ArrayList<>();

        for (Iterator<Product> iterator = productslist.iterator();iterator.hasNext();) {
            ProductAndNum productAndNum = new ProductAndNum();
            Product produ = iterator.next();
            productAndNum.setProductId(produ.getProductId());
            productAndNum.setProductName(produ.getProductName());

            for (Iterator<Stock> iterators = stockListNext.iterator();iterators.hasNext();) {

                Stock sto = iterators.next();

                System.out.println(sto);

                if(productAndNum.getProductId().equals(sto.getProductId())) {
                    productAndNum.setNum(sto.getNum());
                }
            }


            productAndNums.add(productAndNum);
        }

        return Result.success(productAndNums);
    }

    /**
     * 商品下拉框接口
     * @author zhennan.liu
     * @param productAndUser
     * @return
     */
    @PostMapping("/product_name/list/g")
    public Result getall(
            @RequestBody ProductAndUser productAndUser){

            // 通过产品名称和产品类型获取产品id
            Integer productId = iProductService.selectByTypeName(productAndUser.getType(), productAndUser.getProductName());

            // 通过管理员id获取仓库id
            Integer warehouseId = iWarehouseService.selectByid(productAndUser.getUserId());

            // 通过产品id和仓库id查询一个库存对象
            return Result.success(iStockService.selectAllByid(productId, warehouseId));
    }

    /**
     * 获取所有商品
     * @param
     * @author wei.zhang
     * @return
     */
    @PostMapping("/type/list/g")
    public Result selectType(){

        //构建工资等级查询条件构造器
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();

        //将商品类型分组查询所有（类别去重）
        productQueryWrapper.groupBy(DaoConstant.PRODUCT.TYPE);

        //查询没有被下架的商品
        productQueryWrapper.eq(DaoConstant.PRODUCT.STATE, "Y");

        //符合条件所有集合
        List<Product> products = iProductService.list(productQueryWrapper);

        // 新建一个空的ArrayList
        ArrayList<String> productTypes = new ArrayList<>();

        // 循环插入商品类型
        for (Product product: products) {
            productTypes.add(product.getType());
        }

        // 返回查询结果
        return Result.success(productTypes);

    }
}

